package com.yeyaomai.dksns.flowis.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.yeyaomai.dksns.flowis.service.DownExcelService;
import com.yeyaomai.dksns.flowis.util.ListPropertyAndValueTool;

/**
 * @author dingtao@yeyaomai.com
 * 
 */

@Service
public class DownExcelServiceImpl implements DownExcelService {

	/**
	 *  TODO 注意 pros的属性顺序和 header 要一一 对应
	 * 
	 */
	@Override
	public <T> void exportExcel(String title, List<String> header,
			List<T> dateSet, OutputStream out, List<String> pros) {

		HSSFWorkbook workbook = new HSSFWorkbook();

		HSSFSheet sheet = workbook.createSheet(title);
		
		sheet.setDefaultColumnWidth(15);

		// 第一列 
		HSSFRow row = sheet.createRow(0);

		for (int i = 0; i < header.size(); i++) {
			HSSFCell cell = row.createCell(i);

			HSSFRichTextString text = new HSSFRichTextString(header.get(i));

			cell.setCellValue(text);
		}

		
		for(int j=1;j<dateSet.size()+1;j++)
		{
		   row = sheet.createRow(j);
		   List<String> values=	ListPropertyAndValueTool.display(dateSet.get(j-1), pros);
		
		   for(int k=0;k<values.size();k++)
		   {
			   HSSFCell cell=   row.createCell(k);
			   
			   HSSFRichTextString text = new HSSFRichTextString(values.get(k));
			   cell.setCellValue(text);
		   }
		
		}
		
		try {
			workbook.write(out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace(); // TODO  异常怎么处理
		}

	}



}
